package com.e3.basdoc.bank.server;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;

import com.e3.basdoc.bank.client.BankHelper;
import com.e3.basdoc.bank.client.BankRPCService;
import com.e3.basdoc.bank.model.BankModel;
import com.e3.basdoc.bank.model.BankQC;
import com.e3.basdoc.region.model.RegionModel;
import com.e3.basdoc.region.server.RegionServiceClient;
import com.e3.core.base.ServiceException;
import com.e3.sysmg.audit.log.MyLoggerFactory;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class BankRPCServiceImpl extends GwtRPCAction implements BankRPCService {
	
	Logger logger = MyLoggerFactory.getLogger(BankRPCServiceImpl.class);
	
	/**
	 * 
	 */
	public List<BankModel> getBankChildren(final BankModel bankModel) throws WebException {
		return new GwtRPCExecutor<List<BankModel>>() {
			@Override
			public List<BankModel> execute() throws ServiceException, Exception {
				List<BankModel> models = new ArrayList<BankModel>();
				if(bankModel == null){
					BankModel model = BankHelper.INSTANCE.createRootModel();
					models.add(model);
					return models;
				}
				
				BankModel qModel = bankModel;
				if(BankHelper.INSTANCE.isRootModel(qModel)){
					qModel = null;
				}
				
				models = BankServiceClient.getBankQueryService().getBankChildren(qModel);
				fillRegionModel(models);
				return models;
			}
		}.doExecute();
	}

	public BankModel newBank(final BankModel bankModel) throws WebException {
		return new GwtRPCExecutor<BankModel>() {

			@Override
			public BankModel execute() throws ServiceException, Exception {
				return BankServiceClient.getBankService().newBank(bankModel);
			}

		}.doExecute();
	}

	public BankModel modifyBank(final BankModel bankModel) throws WebException {
		return new GwtRPCExecutor<BankModel>() {

			@Override
			public BankModel execute() throws ServiceException, Exception {
				return BankServiceClient.getBankService().modifyBank(bankModel);
			}

		}.doExecute();
	}

	public List<BankModel> getBankByCondition(final BankQC bankQC) throws WebException {
		return new GwtRPCExecutor<List<BankModel>>() {

			@Override
			public List<BankModel> execute() throws ServiceException, Exception {
				List<BankModel> banks = BankServiceClient.getBankQueryService().getBankByCondition(bankQC);
				if (banks == null || banks.isEmpty()) {
					return banks;
				}
				//fillParentBank(banks);
				fillRegionModel(banks);
				return banks;
			}

		}.doExecute();
	}

	protected void fillParentBank(List<BankModel> banks) throws Exception{
		for (BankModel bankModel : banks) {
			if(bankModel.getParentBank()!=null){
				continue;
			}
			long id = bankModel.getParentId();
			if(id == 0){
				continue;
			}
			BankModel pModel = BankServiceClient.getBankQueryService().getBankById(id);
			bankModel.setParentBank(pModel);
		}
	}

	public void deleteBanks(final List<BankModel> banks) throws WebException {
		new GwtRPCVoidExecutor() {

			@Override
			public void execute() throws ServiceException, Exception {
				if(banks == null||banks.isEmpty()){
					return;
				}
				List<Long> ids = new ArrayList<Long>(banks.size());
				for (BankModel bankModel : banks) {
					long id = bankModel.getParentId();
					ids.add(id);
				}
				BankServiceClient.getBankService().deleteBankByIds(ids);
			}

		}.doExecute();
	}
	
	public void fillRegionModel(List<BankModel> models) throws ServiceException{
		if(models==null||models.isEmpty()){
			return;
		}
		for (BankModel bankModel : models) {
			if(bankModel.getRegionModel()!=null){
				continue;
			}
			Long id = bankModel.getRegionId();
			RegionModel rm = RegionServiceClient.getRegionQueryService().getRegionModelById(id);
			if(rm==null){
				logger.error("Unkown Region {}",id);
			}
			bankModel.setRegionModel(rm);
		}

	}
}
